y variable defined on the left remains the same
The p-value of the test is less than the significance level alpha = 0.05. We can conclude that hr sleep and stress sleep are significantly correlated with a correlation coefficient beetween 0.64–0.88.
The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
The p-value of the test is less (2 participants) than the significance level alpha = 0.05. We can conclude that variable are significantly correlated .
n= 122
node), split, n, loss, yval, (yprob)
* denotes terminal node
1) root 122 24 0 (0.80327869 0.19672131)
2) total_sleep_hours>=7.233333 89 10 0 (0.88764045 0.11235955)
4) light_sleep_percentage< 0.7336932 82 6 0 (0.92682927 0.07317073)
8) light_sleep_percentage>=0.547778 68 2 0 (0.97058824 0.02941176) *
9) light_sleep_percentage< 0.547778 14 4 0 (0.71428571 0.28571429)
18) awake_percentage< 0.0137832 10 1 0 (0.90000000 0.10000000) *
19) awake_percentage>=0.0137832 4 1 1 (0.25000000 0.75000000) *
5) light_sleep_percentage>=0.7336932 7 3 1 (0.42857143 0.57142857) *
3) total_sleep_hours< 7.233333 33 14 0 (0.57575758 0.42424242)
6) light_sleep_percentage< 0.5924029 15 2 0 (0.86666667 0.13333333) *
7) light_sleep_percentage>=0.5924029 18 6 1 (0.33333333 0.66666667)
14) %_stress_awake>=0.4945208 8 2 0 (0.75000000 0.25000000) *
15) %_stress_awake< 0.4945208 10 0 1 (0.00000000 1.00000000) *
Call:
glm(formula = bad_sleep_outlier_binary ~ ., family = binomial(link = "logit"),
data = train)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.84699 -0.59669 -0.32440 -0.07701 2.60505
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 8.996e+01 9.640e+03 0.009 0.9926
total_sleep_hours -2.173e-01 3.408e-01 -0.638 0.5237
light_sleep_percentage -7.837e+01 9.640e+03 -0.008 0.9935
deep_sleep_percentage -8.151e+01 9.640e+03 -0.008 0.9933
rem_sleep_percentage -1.016e+02 9.640e+03 -0.011 0.9916
awake_percentage 8.162e+00 5.842e+00 1.397 0.1624
mean_hr_sleep 1.490e-01 4.664e-01 0.319 0.7494
median_hr_sleep -2.493e-01 4.472e-01 -0.557 0.5773
activity_calories -1.154e-02 5.419e-03 -2.129 0.0333 *
activity_hours 9.520e-01 1.077e+00 0.884 0.3768
steps -1.652e-05 2.505e-04 -0.066 0.9474
distance 1.247e-04 1.094e-04 1.139 0.2545
moderate_activity_hours 1.037e+00 2.077e+00 0.499 0.6177
vigorous_activity_hours 6.240e+00 3.770e+00 1.655 0.0979 .
floors_climbed 2.292e-03 2.222e-02 0.103 0.9178
mean_hr_awake -1.247e-01 1.375e-01 -0.907 0.3646
`%_stress_awake` 9.278e-01 2.328e+00 0.398 0.6903
mean_hr_30min_before_bedtime 1.008e-01 7.084e-02 1.423 0.1547
`%_stress_30min_before_bedtime` -8.865e-01 1.303e+00 -0.680 0.4963
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 116.412 on 117 degrees of freedom
Residual deviance: 79.664 on 99 degrees of freedom
AIC: 117.66
Number of Fisher Scoring iterations: 14
| bad_sleep_outlier_binary | |||
|---|---|---|---|
| Predictors | Odds Ratios | CI | p |
| (Intercept) | 1169300010960153958143778830079339003904.00 | 0.00 – NA | 0.993 |
| total sleep hours | 0.80 | 0.40 – 1.54 | 0.524 |
| light sleep percentage | 0.00 | NA – Inf | 0.994 |
| deep sleep percentage | 0.00 | NA – Inf | 0.993 |
| rem sleep percentage | 0.00 | NA – Inf | 0.992 |
| awake percentage | 3504.56 | 0.04 – 845308231.22 | 0.162 |
| mean hr sleep | 1.16 | 0.45 – 2.92 | 0.749 |
| median hr sleep | 0.78 | 0.32 – 1.91 | 0.577 |
| activity calories | 0.99 | 0.98 – 1.00 | 0.033 |
| activity hours | 2.59 | 0.31 – 23.15 | 0.377 |
| steps | 1.00 | 1.00 – 1.00 | 0.947 |
| distance | 1.00 | 1.00 – 1.00 | 0.255 |
| moderate activity hours | 2.82 | 0.03 – 133.25 | 0.618 |
| vigorous activity hours | 512.64 | 0.39 – 1624456.27 | 0.098 |
| floors climbed | 1.00 | 0.95 – 1.05 | 0.918 |
| mean hr awake | 0.88 | 0.70 – 1.16 | 0.365 |
| % stress awake | 2.53 | 0.02 – 121.58 | 0.690 |
|
mean hr 30min before bedtime |
1.11 | 0.96 – 1.28 | 0.155 |
|
% stress 30min before bedtime |
0.41 | 0.03 – 3.45 | 0.496 |
| Observations | 118 | ||
| R2 Tjur | 0.323 | ||
---
title: "Sleep Dashboard "
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
theme:
bg: "#ffffff"
fg: "#000000"
primary: "#ED79F9"
logo: logo.png
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(tidyverse)
library(hrbrthemes)
library(viridis)
library(lubridate)
library(reshape2)
library(ggplot2)
library(ggpubr)
library(plotly)
library(GGally)
setwd("~/Desktop/module_3/all_data")
Lorena <- read_csv("Lorena_converted (3).csv")
Lorena$ID<-"W1"
Martin <- read_csv2("Martin.csv")
Martin$ID<-"M1"
Nely <- read_csv2("Nely.csv")
Nely$ID<-"W2"
dd<-merge(Nely, Martin, all = TRUE)
dd<-merge(dd, Lorena, all = TRUE)
```
Overview
=======================================================================
Row
-----------------------------------------------------------------------
### Global sleep overview (3 subjects Night Owl Early Bird Sleepy Bear)
```{r}
dd1<-dd[,c(1,11,12,13,14,98)]
dd1<-as.data.frame(dd1)
dd1<-melt(dd1, id=c("calendar_date","ID"))
p<-ggplot(dd1, aes(x=calendar_date,y=round(value*100),group=variable, fill=variable)) +
geom_density(stat = "identity",alpha=.6)+theme_ipsum() +scale_fill_ipsum()+
labs(x = "Date",
y = "Sleep in %",
color = "Sleep variable")+facet_grid(~ID)
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))
```
>y variable defined on the left remains the same
Row
-----------------------------------------------------------------------
### total sleep overview
```{r }
p<-ggplot(dd, aes(x = calendar_date, y = total_sleep_hours)) +
geom_line()+geom_point()+geom_smooth()+theme_ipsum()+facet_grid(~ID)
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))
```
Row
-----------------------------------------------------------------------
### total sleep overview (by day of week)
```{r }
dd$day<-weekdays(dd$calendar_date,abbreviate = TRUE)
dd$day<-as.factor(dd$day)
dd$day2<-dd$day
levels(dd$day2) <- c("Sunday","Thursday", "Monday", "Tuesday", "Wednesday", "Saturday",
"Friday")
dd$day2 <- ordered(dd$day2, levels =c("Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"))
p<-dd %>%
ggplot( aes(x=day2,y = total_sleep_hours,fill=ID)) +
geom_boxplot() + theme_ipsum() +scale_fill_ipsum()
p %>%
ggplotly() %>%
layout(boxmode = "group")
```
### deep sleep variation (by day of week)
```{r }
p<-dd %>%
ggplot( aes(x=day2,y = deep_sleep_percentage,fill=ID)) +
geom_boxplot() + theme_ipsum() +scale_fill_ipsum()
p %>%
ggplotly() %>%
layout(boxmode = "group")
```
Row
-----------------------------------------------------------------------
### _stress_awake (by day of week)
```{r }
p<-dd %>%
ggplot( aes(x=day2,y = dd$`%_stress_awake`,fill=ID)) +
geom_boxplot() + theme_ipsum() +scale_fill_ipsum()
p %>%
ggplotly() %>%
layout(boxmode = "group")
```
### _stress_awake
```{r }
p<-ggplot(dd, aes(x=calendar_date,y=round(median_stress_awake))) +
geom_smooth()+facet_grid(~ID)+theme_ipsum()
ggplotly(p)
```
Corr_Night analysis
=======================================================================
Row
-----------------------------------------------------------------------
### **Influence from Median_hr_sleep on sleep quality**
```{r }
p<- ggplot(dd,aes(x=perceived_sleep_quality, y=median_hr_sleep, fill=factor(ID))) +
geom_boxplot() +geom_point()+
scale_fill_ipsum()+
labs(x = "perceived_sleep_quality by subject",
y = "median_hr_sleep",
color = "Sleep variable")+ theme_ipsum() +scale_fill_ipsum()
ggplotly(p)%>%
layout(boxmode = "group")
```
### **Influence from median_stress_sleep on sleep quality**
```{r}
p<- ggplot(dd,aes(x=perceived_sleep_quality, y=median_stress_sleep, fill=factor(perceived_sleep_quality))) +
geom_boxplot() +geom_point()+
scale_fill_ipsum()+
labs(x = "perceived_sleep_quality",
y = "median_stress_sleep",
color = "Sleep variable")+ theme_ipsum() +scale_fill_ipsum()
ggplotly(p)%>% layout(legend = list(orientation = "h", y = -0.2))
```
Row
-----------------------------------------------------------------------
### **Correlation stress & HR**
```{r}
p<-ggscatter(dd, x = "median_stress_sleep", y = "median_hr_sleep", color="ID",
add = "reg.line", conf.int = FALSE,
cor.coef = FALSE, cor.method = "pearson")+
stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```
> The p-value of the test is less than the significance level alpha = 0.05. We can conclude that hr sleep and stress sleep are significantly correlated with a correlation coefficient beetween 0.64--0.88.
### **Correlation vigorous_activity_hours & total_sleep_hours**
```{r}
p<-ggscatter(dd, x = "vigorous_activity_hours", y = "total_sleep_hours", color="ID",
add = "reg.line", conf.int = FALSE,
cor.coef = FALSE, cor.method = "pearson")+
stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```
>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
### **Correlation steps & total_sleep_hours**
```{r}
p<-ggscatter(dd, x = "steps", y = "total_sleep_hours", color="ID",
add = "reg.line", conf.int = FALSE,
cor.coef = FALSE, cor.method = "pearson")+
stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```
>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
Row
-----------------------------------------------------------------------
### **Correlation %_high_stress_30min_before_bedtime & total_sleep_hours**
```{r}
p<-ggscatter(dd, x = "%_high_stress_30min_before_bedtime", y = "total_sleep_hours", color="ID",
add = "reg.line", conf.int = FALSE,
cor.coef = FALSE, cor.method = "pearson")+
stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```
>The p-value of the test is more than the significance level alpha = 0.05. We can conclude that variable are not significantly correlated .
### **Correlation estimated_sleep_time & total_sleep_hours**
```{r}
p<-ggscatter(dd, x = "estimated_sleep_time", y = "total_sleep_hours", color="ID",
add = "reg.line", conf.int = FALSE,
cor.coef = FALSE, cor.method = "pearson")+
stat_cor(aes(color = ID), p.accuracy = 0.001, r.accuracy = 0.01)+ theme_ipsum()+scale_fill_ipsum()
p
```
>The p-value of the test is less (2 participants) than the significance level alpha = 0.05. We can conclude that variable are significantly correlated .
Global corr analysis & ML Models
=======================================================================
Row
-----------------------------------------------------------------------
### **Correlation **
```{r }
dd2<-dd[,c(6,42,69,97)]
ggpairs(dd2)
```
Row {}
-----------------------------------------------------------------------
### **ML decision tree**
```{r message=FALSE, warning=FALSE}
library(rpart)
library(rpart.plot)
library(Metrics)
## Trainingfsfunktion
## für Visualisierung (prp)
## Performance Metriken (accuracy)
#Test - Train - Split (Details dazu später)
data<-dd
##Variable selection
data<-dd[,c(6,11:16,40:47,69,70,92,97)]
data$bad_sleep_outlier_binary<-as.factor(data$bad_sleep_outlier_binary)
#train & test model
in_train <- sample(nrow(data), trunc(nrow(data) * 2 / 3))
train <- data[in_train,]
test <- data[-in_train,]
# Model formul
L_form <- bad_sleep_outlier_binary ~ .
model <- rpart(L_form, train,method = "class", minsplit = 10, minbucket = 3)
model
prp(model)
rpart.plot(model, extra= 106)
```
Row {}
-----------------------------------------------------------------------
### **ML using glm( with bad_sleep_outlier_binary ~ ., family = binomial(link = "logit")**
```{r message=FALSE, warning=FALSE}
library(sjPlot)
library(sjmisc)
library(sjlabelled)
dd1<-na.omit(data)
in_train <- sample(nrow(dd1), trunc(nrow(dd1) * 2 / 3))
train <- dd1[in_train,]
test <- dd1[-in_train,]
dd1$bad_sleep_outlier_binary<-as.factor((dd1$bad_sleep_outlier_binary))
model <- glm(bad_sleep_outlier_binary ~.,family=binomial(link='logit'),data=train)
summary(model)
tab_model(model,show.intercept = TRUE)
```